package ro.gateway.aida.sectors;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.AIDASector;
import ro.gateway.aida.obj.db.AIDASectorPersMan;
import ro.gateway.aida.utils.HttpUtils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: ro-gateway</p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: FinderAction.java,v 1.1 2004/10/24 23:37:09 mihaipostelnicu Exp $
 */
public class FinderAction extends Action
{
      public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response )
            throws Exception
      {
            ServletContext application = this.getServlet().getServletContext();
            PersistenceToken pers_token = PersistenceManager.tokenLookup( application );
            LanguageBean lang_bean = TranslatorUtils.
                  getCurrentLanguage( request, application );

            SectorChooserForm myform = ( SectorChooserForm ) form;
            myform.clear_objects();
            myform.action = HttpUtils.
                  getValidTrimedString( request, "action", null );

            if ( "save".equals( myform.action ) )
            {
                  AIDASector[] found_sectors = SectorWrapper.unwrapp( myform.found_sectors );
                  if ( ( found_sectors != null )
                        && ( myform.selected_index >= 0 )
                        && ( myform.selected_index < found_sectors.length ) )
                  {
                        AIDASector found_sector = found_sectors[ myform.selected_index ];

                        if ( found_sector.getParent() == null )
                        {
                              myform.sector_id = found_sector.getRo_aida_code();
                              myform.sector = found_sector;
                              myform.subsector = null;
                              myform.subsector_id = -1;
                              myform.subsector1 = null;
                              myform.subsector1_id = -1;
                        }
                        else
                        {
                              if ( found_sector.getParent().getParent() == null )
                              {
                                    myform.sector_id = found_sector.getParent_ro_aida_code();
                                    myform.sector = found_sector.getParent();
                                    myform.subsector_id = found_sector.getRo_aida_code();
                                    myform.subsector = found_sector;
                                    myform.subsector1 = null;
                                    myform.subsector1_id = -1;
                              }
                              else
                              {
                                    myform.sector_id = found_sector.getParent().getParent_ro_aida_code();
                                    myform.sector = found_sector.getParent().getParent();
                                    myform.subsector_id = found_sector.getParent().getRo_aida_code();
                                    myform.subsector = found_sector.getParent();
                                    myform.subsector1_id = found_sector.getRo_aida_code();
                                    myform.subsector1 = found_sector;
                              }
                        }

                        return mapping.findForward( "finnish" );
                  }
            }

            myform.what = HttpUtils.
                  getValidTrimedString( request, "what", null );
            if ( myform.what != null )
            {
                  myform.action = "Search";
            }
            else
            {
                  myform.found_sectors = null;
            }

            if ( "Search".equals( myform.action ) )
            {
                  AIDASectorPersMan pers_man = AIDASectorPersMan.getManager( pers_token );
                  AIDASector[] foundSectors = pers_man.
                        findSectors( myform.what, lang_bean.language );
                  /**
                   * from manuela: searchul merge pt. doua nivele (este un join in selectul respectiv)
                   * deci trebuie sa mai trag un parinte in cazul in care in rezultat sint sectoare
                   * de nivel 3.
                   */
                  if ( foundSectors != null )
                  {
                        for ( int i = 0; i < foundSectors.length; i++ )
                        {
                              if ( foundSectors[ i ].getParent() != null &&
                                    foundSectors[ i ].getParent().getParent_ro_aida_code() != -1 )
                              {
                                    AIDASector grandparent = pers_man.getById(
                                          foundSectors[i].getParent().getParent_ro_aida_code());
                                    if (grandparent != null) {
                                          foundSectors[i].getParent().setParent(grandparent);
                                    }
                              }
                        }
                  }

                  ArrayList items = new ArrayList();
                  if ( foundSectors != null )
                        for ( int i = 0; i < foundSectors.length; i++ )
                        {
                              items.add( foundSectors[ i ] );
                              if ( foundSectors[ i ].getParent() == null )
                              {
                                    AIDASector subsectors[] = pers_man.
                                          getAllByLangForParent( lang_bean.language, foundSectors[ i ].getRo_aida_code() );
                                    if ( subsectors != null )
                                          for ( int j = 0; j < subsectors.length; j++ )
                                          {
                                                subsectors[ j ].setParent( foundSectors[ i ] );
                                                items.add( subsectors[ j ] );
                                                AIDASector subsectors1[] = pers_man.
                                                      getAllByLangForParent( lang_bean.language, subsectors[ j ].getRo_aida_code() );
                                                if ( subsectors1 != null )
                                                      for ( int k = 0; k < subsectors1.length; k++ )
                                                      {
                                                            subsectors1[ k ].setParent( subsectors[ j ] );
                                                            items.add( subsectors1[ k ] );
                                                      }
                                          }
                              }
                        }

                  for ( int i = 0; i < items.size(); i++ )
                  {
                        AIDASector aidaSector = ( AIDASector ) items.get( i );
                        for ( int j = i + 1; j < items.size(); j++ )
                        {
                              AIDASector aidaSector1 = ( AIDASector ) items.get( j );
                              if ( aidaSector.getRo_aida_code() == aidaSector1.getRo_aida_code() )
                              {
                                    items.remove( i-- );
                                    break;
                              }
                        }
                  }

                  SectorWrapper wrappedSectors[] = null;
                  if ( items.size() > 0 )
                  {
                        AIDASector[] sectors_ = new AIDASector[ items.size() ];
                        items.toArray( sectors_ );
                        wrappedSectors = SectorWrapper.wrapp( sectors_, lang_bean.language );
                  }
                  myform.setFound_sectors( wrappedSectors );

                  if ( wrappedSectors == null )
                  {
                        request.setAttribute( "search_failed", "true" );
                  }


                  myform.selected_index = -1;
            }

            return mapping.findForward( "finder_jsp" );
      }
}
